<template>
  <div class="test-container">
    <div class="top-main">
      <search :listQuery="listQuery" :path="$route.name"></search>
    </div>
    <div class="bottom-main">
      <div class="list-button-group">
        <div class="button-item">
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </div>
      </div>
      <div class="table-list">
        <el-table
          v-loading="listLoading"
          :data="list"
          :element-loading-text="elementLoadingText"
          @selection-change="setSelectRows"
          highlight-current-row
          height="100%"
          :header-cell-style="{
            'border-bottom': '1px solid #cbcccd',
            'border-top': '1px solid #cbcccd',
          }"
        >
          <el-table-column
            show-overflow-tooltip
            type="selection"
          ></el-table-column>
          <el-table-column show-overflow-tooltip label="序号" width="50">
            <template #default="scope">
              {{
                scope.$index +
                listQuery.pageSize * (listQuery.pageIndex - 1) +
                1
              }}
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="pandiandh"
            label="盘点单号"
            width="190"
          ></el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="mingcheng"
            label="名称"
          ></el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="zhuangtai"
            label="状态"
            width="200"
          >
            <template slot-scope="scope">
              {{ $util.getpddzt(scope.row.zhuangtai) }}
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="tips"
            label="备注"
          ></el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="createName"
            label="创建人"
            width="190"
          ></el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="createTime"
            label="创建时间"
            width="190"
          ></el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="操作"
            fixed="right"
            min-width="150"
          >
            <template #default="{ row }">
              <el-button type="text" @click="showdetail(row)">明细</el-button>
              <el-button
                type="text"
                @click="edit(row)"
                v-if="row.zhuangtai != 3"
              >
                编辑
              </el-button>
              <el-button
                type="text"
                @click="handleDelete(row)"
                v-if="row.zhuangtai != 2"
              >
                删除
              </el-button>
              <el-dropdown
                @command="updateStatuPdd(row, $event)"
                trigger="click"
              >
                <span class="el-dropdown-link">
                  更多
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="1">新单待盘点</el-dropdown-item>
                  <el-dropdown-item :command="2">进行中</el-dropdown-item>
                  <el-dropdown-item :command="3">盘点完成</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        background
        :current-page="listQuery.pageIndex"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :layout="layout"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <add
      v-if="addshow"
      :gobacktitle="gobacktitle"
      :datarow="gobacktitle == '新增' ? {} : editData"
    ></add>
    <detail
      v-if="detailshow"
      :gobacktitle="gobacktitle"
      :datarow="detailrow"
    ></detail>
  </div>
</template>

<script>
  let _listQuery = {
    pageIndex: 1,
    pageSize: 20,
  }
  import search from './components/search.vue'
  import {
    queryPandiandList,
    queryPandiandById,
    deletePandiandByIds,
    updatePandiandAndMx,
  } from '@/api/kunei'
  import Detail from './components/page1detail.vue'
  import add from './components/page1Add.vue'

  export default {
    name: 'WarehouseManagement',
    components: { add, search, Detail },
    data() {
      return {
        listQuery: Object.assign({}, _listQuery),
        list: null,
        listLoading: false,
        layout: 'total,sizes,prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        selectrow: [],
        elementLoadingText: '正在加载...',
        detailrow: {},
        detailshow: false,
        addshow: false,
        editData: null,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      updateStatuPdd(row, command) {
        if (row.zhuangtai == command) {
          this.$baseMessage('当前状态与操作一致，无需修改', 'error')
        } else {
          updatePandiandAndMx({
            id: row.id,
            zhuangtai: command,
          }).then((res) => {
            this.$baseMessage(res.msg, 'success')
            this.fetchData()
          })
        }
      },
      setSelectRows(val) {
        this.selectrow = []
        val.forEach((i) => {
          this.selectrow.push(i.id)
        })
      },
      handleDelete(row) {
        this.$confirm('确认删除选中的数据吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          const RKDdata = await deletePandiandByIds({ id: row.id })

          this.fetchData()
          this.$message({
            message: '删除成功',
            type: 'success',
          })
        })
      },

      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.pageIndex = val
        this.fetchData()
      },
      queryData() {
        this.listQuery.pageIndex = 1
        this.fetchData()
      },
      add() {
        this.addshow = true
        this.gobacktitle = '新增'
      },
      async edit(row) {
        const mxData = await queryPandiandById({ id: row.id })
        this.addshow = true
        this.gobacktitle = '编辑'
        this.editData = mxData.data
      },
      async showdetail(row) {
        const mxData = await queryPandiandById({ id: row.id })
        this.detailshow = true
        this.gobacktitle = '盘点单明细'
        this.detailrow = mxData.data
      },
      async fetchData() {
        const res = await queryPandiandList(this.listQuery)
        this.list = res.data.rows
        this.total = res.data.total
      },
      search() {
        //搜索
        this.listQuery.pageIndex = 1
        this.fetchData()
      },
      resetlist() {
        //重置
        this.listQuery = Object.assign({}, _listQuery)
        this.fetchData()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .test-container {
    display: flex;
    flex-direction: column;
    padding: 0 !important;
    background: #f6f8f9 !important;
    height: 100%;

    .top-main {
      width: 100%;
      background: #fff;
      padding: 10px 0;
    }

    .bottom-main {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      background-color: #fff;
      margin-top: 10px;
      padding-bottom: 10px;

      .list-button-group {
        display: flex;
        flex-direction: row;
        padding: 20px;

        .button-item {
          padding: 0px 10px;
        }

        .button-item:first-child,
        &:last-child {
          padding: 0px 10px 0 0;
        }
      }

      .table-list {
        flex: 1;
        padding: 0 20px 20px;
      }
    }

    .el-button {
      font-size: 0.14rem !important;
    }
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
    margin-left: 10px;
  }

  .el-icon-arrow-down {
    font-size: 14px;
  }
</style>
